<?php
/**
 * @author      :   DungCV
 * @name        :   Fpt_Authenticate
 * @version     :   201103
 * @copyright   :   My company
 * @todo        :   Using to authenticate
 */
class Fpt_Authenticate implements Zend_Auth_Adapter_Interface {

    protected $_username;
    protected $_phassphrase;

    /**
     * Sets username and password for authentication
     *
     * @return void
     */
    public function __construct($username, $phassphrase) {
        $this->_username    = $username;
        $this->_phassphrase = $phassphrase;
    }

    /**
     * Performs an authentication attempt
     *
     * @throws Zend_Auth_Adapter_Exception If authentication cannot
     *                                     be performed
     * @return Zend_Auth_Result
     */
    public function authenticate() {
        $userInstance = User::getInstance();

        //Check username
        $user = $userInstance->getDetailByUsername($this->_username);
        if (!$user) {
            return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, $user);
        }

        //Check password
        if($user['phassphrase'] != md5($this->_phassphrase)) {
            return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, $user);
        }

        return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $user);
    }

}